package com.ssy.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ssy.entity.Clazz;
import com.ssy.entity.ClazzQueryParam;
import com.ssy.entity.PageBean;
import com.ssy.mapper.ClazzMapper;
import com.ssy.mapper.EmpMapper;
import com.ssy.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ClazzServiceImpl implements ClazzService {

    @Autowired
    private ClazzMapper clazzMapper;

    /**
     * 分页查询班级信息(及根据条件进行查询)
     * @param clazzQueryParam
     * @return
     */
    @Override
    public PageBean list(ClazzQueryParam clazzQueryParam) {
        PageHelper.startPage(clazzQueryParam.getPage(), clazzQueryParam.getPageSize());

        List<Clazz> clazzPage = clazzMapper.page(clazzQueryParam);
        Page<Clazz> p = (Page<Clazz>) clazzPage;

        return new PageBean(p.getTotal(), p.getResult());
    }

    /**
     * 添加班级信息
     */
    @Override
    public void save(Clazz clazz) {
        // 补全数据(创建时间与修改时间)
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
        //
        clazzMapper.insert(clazz);
    }

    /**
     * 删除班级信息
     * @param id
     */
    @Override
    public void delete(Integer id) {
        clazzMapper.delete(id);
    }

    /**
     * 根据id查询班级信息
     * @param id
     * @return
     */
    @Override
    public Clazz getById(Integer id) {
        return clazzMapper.getById(id);
    }

    /**
     * 修改班级信息
     * @param clazz
     */
    @Override
    public void update(Clazz clazz) {
        // 补全修改时间信息
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.updateById(clazz);
    }

    /**
     * 查询所有班级信息
     */
    @Override
    public List<Clazz> listAll() {
        List<Clazz> clazz = clazzMapper.listAll();
        return clazz;
    }
}
